package com.kkb.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

@ControllerAdvice   //该注解定义全局异常处理类
public class GlobalExceptionHandler {
    private static Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    @ExceptionHandler(value = Exception.class)
    public String exceptionHandler(Model model, Exception e) {
        e.printStackTrace();
        //异常处理
        logger.error(e.getMessage(), e);
        //向页面传递数据
        model.addAttribute("message", e.getMessage());
        //跳转到错误页面
        return "error";
    }
}
